package com.thinkingcoder.tcsharding.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

import com.thinkingcoder.tcsharding.config.ShardingAutoConfiguration;
import com.thinkingcoder.tcsharding.demo.mapper.UserMapper;
import com.thinkingcoder.tcsharding.demo.model.User;
import com.thinkingcoder.tcsharding.orm.mybatis.ShardingMapperFactoryBean;

@SpringBootApplication
@Import({ShardingAutoConfiguration.class})
@MapperScan(value = "com.thinkingcoder.tcsharding.demo.mapper", factoryBean = ShardingMapperFactoryBean.class)
public class TcShardingApplication {

    public static void main(String[] args) {
        SpringApplication.run(TcShardingApplication.class, args);
    }

    @Autowired
    private UserMapper userMapper;

    @Bean
    ApplicationRunner runner() {
        return x -> {
            for (int i = 1; i <= 3; i++) {
                test(i);
            }
        };
    }

    private void test(int i) {
        System.out.println(" ===> 1. test insert ...");
        int insertResult = userMapper.insert(new User(i, "coder", 31 + i));
        System.out.println(" ===> 1. insert result: " + insertResult);

        // System.out.println(" ===> 2. test findById ...");
        // User findByIdResult = userMapper.findById(i);
        // System.out.println(" ===> 2. findById result: " + findByIdResult);
        //
        // System.out.println(" ===> 3. test update ...");
        // int updateResult = userMapper.update(new User(i, "coder-update", 34 + i));
        // User updatedResult = userMapper.findById(i);
        // System.out.println(" ===> 3. update result: " + updateResult + " | " + updatedResult);
        //
        // System.out.println(" ===> 4. test delete ...");
        // int deleteResult = userMapper.delete(i);
        // System.out.println(" ===> 4. delete result: " + deleteResult);
    }

}
